#include "stm32f10x.h"                  // 设备头文件
#include "OLED.h"
#include "KEY.h"
#include "Servo.h"
#include "PWM.h"
#include "serial.h"  
uint8_t  KeyNum;
uint8_t  flag = 1;
uint8_t D=2;					//用于存储目前按键是上还是下,默认下一项

int menu1(void)
{	
	OLED_ShowString(0,0 , "开始追踪          		",OLED_8X16);
	OLED_ShowString(0,16, "停止运动                ",OLED_8X16);
	OLED_ShowString(0,32, "回到原点          		",OLED_8X16);
	OLED_Update();	
	
	while(1)
	{
		KeyNum = Key_GetNum();
		if(KeyNum == 1)//上一项
		{
			flag--;
			if(flag == 0){flag = 4;}
			D = 1;//上一项标志
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果
		}
		if(KeyNum == 2)//下一项
		{
			flag++;
			if(flag == 5){flag = 1;}
			D = 2;//下一项标志
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果
		}
		if(KeyNum == 3)//确认
		{
			OLED_Clear();
			OLED_Update();
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果
			return flag;
		}
		
		switch(flag)
		{	
			case 1:
			{
				OLED_ShowString(0,0 , "开始追踪          		",OLED_8X16);
				OLED_ShowString(0,16, "停止运动                ",OLED_8X16);
				OLED_ShowString(0,32, "回到原点          		",OLED_8X16);
		
				if(D==2){OLED_Animation(0,0,0,0,0,0,64,16);}//下
				if(D==1){OLED_Animation(0,16,64,16,0,0,64,16);}//上
				
				break;
			}
			case 2:{
				//再次显示,解决乱码和闪烁问题(确保反相前是亮的状态)
				OLED_ShowString(0,0 , "开始追踪          		",OLED_8X16);
				OLED_ShowString(0,16, "停止运动                ",OLED_8X16);
				OLED_ShowString(0,32, "回到原点          		",OLED_8X16);
				
				if(D==2){OLED_Animation(0,0,64,16,0,16,64,16);}
				if(D==1){OLED_Animation(0,32,64,16,0,16,64,16);}
				
				break;
			}
			case 3:{
				//再次显示,解决乱码和闪烁问题(确保反相前是亮的状态)
				OLED_ShowString(0,0 , "开始追踪          		",OLED_8X16);
				OLED_ShowString(0,16, "停止运动                ",OLED_8X16);
				OLED_ShowString(0,32, "回到原点          		",OLED_8X16);
			
				if(D==2){OLED_Animation(0,16,64,16,0,32,64,16);}
				if(D==1){OLED_Animation(0,48,64,16,0,32,64,16);}
				
			}
			case 4:{
				//再次显示,解决乱码和闪烁问题(确保反相前是亮的状态)
				OLED_ShowString(0,0 , "开始追踪          		",OLED_8X16);
				OLED_ShowString(0,16, "停止运动                ",OLED_8X16);
				OLED_ShowString(0,32, "回到原点          		",OLED_8X16);
		
				if(D==2){OLED_Animation(0,32,64,16,0,48,56,16);}
				if(D==1){OLED_Animation(0,64,56,16,0,48,56,16);}
				break;
			}

				
		}
	}
}  
/**
  * @brief 三级菜单步长设置
  * @param  无
  * @retval 
  */
int menu3_Start(void){	
	int menu3_Startmotor=0;
	uint8_t moflag = 1;//默认在第一行
	OLED_ShowString(0,0,"停止运动            ",OLED_8X16);
	OLED_Printf(0,16,OLED_8X16,"绿色坐标:%d,%d      ",Green_x,Green_y);
	OLED_Printf(0,32,OLED_8X16,"红色坐标:%d,%d      ",Red_x,Red_y);	
	OLED_Animation(0,0,0,0,0,0,64,16);	
	OLED_Update();
	while(1)
	{		
		KeyNum = Key_GetNum();
		if(KeyNum == 1){
			D = 1;//上一项标志
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果
		}
		if(KeyNum == 2){	
			D = 2;//下一项标志
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果
		}
		if(KeyNum == 3){
			OLED_Clear();
			OLED_Update();
			menu3_Startmotor = moflag;
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果	
		}
		if(menu3_Startmotor == 1){return 0;}//返回0,退到第一级菜单
		switch(moflag)
		{
			case 1:
			{
				OLED_ShowString(0,0,"停止运动            ",OLED_8X16);
				OLED_ReverseArea(0,0,64,16);	
				OLED_Printf(0,16,OLED_8X16,"绿色坐标:%d,%d      ",Green_x,Green_y);
				OLED_Printf(0,32,OLED_8X16,"红色坐标:%d,%d      ",Red_x,Red_y);			
				OLED_Update();	
				PID_Task_Execute();					
				break;
			}
		}
	}
}
int menu3_Stop(void)
{
	int menu3_Stopmotor=0;
	uint8_t moflag = 1;//默认在第一行
	OLED_ShowString(0,0,"<-            ",OLED_8X16);
	OLED_Printf(0,16,OLED_8X16,"It has stopped    ");
		
	OLED_Update();
	while(1)
	{	
		PID_stop();
		KeyNum = Key_GetNum();
		if(KeyNum == 1)
		{
			D = 1;//上一项标志
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果
		}
		if(KeyNum == 2)
		{	
			D = 2;//下一项标志
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果
		}
		if(KeyNum == 3)
		{
			OLED_Clear();
			OLED_Update();
			menu3_Stopmotor = moflag;
			OLED_AnimUpdate();//按下按钮,刷新一次动画效果
		}
		if(menu3_Stopmotor == 1){return 0;}//返回0,退到第一级菜单
		switch(moflag)
		{
			//再次显示,解决乱码和闪烁问题(确保反相前是亮的状态)
			case 1:
			{
				OLED_ShowString(0,0,"<-            ",OLED_8X16);
				OLED_Printf(0,16,OLED_8X16,"It has stopped    ");
				if(D==2){OLED_Animation(0,0,0,0,0,0,16,16);}//下
				if(D==1){OLED_Animation(0,16,64,16,0,0,16,16);}//上
				break;
			}
		}
	}
}